/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.AddGroup;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdMetadataManagementBo;
import com.comac.ins.isfd.domain.vo.IsfdMetadataManagementTableInfoVo;
import com.comac.ins.isfd.domain.vo.IsfdMetadataManagementVo;
import com.comac.ins.isfd.service.IIsfdMetadataManagementService;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 元数据管理-数据管理
 *
 * @author Lion Li
 * @date 2024-09-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/metadataManagement")
public class IsfdMetadataManagementController extends BaseController {

    @Autowired
    private IIsfdMetadataManagementService isfdMetadataManagementService;

    /**
     * 查询元数据管理-数据管理列表
     */
    @SaCheckPermission("isfd:metadataManagement:list")
    @GetMapping("/list")
    public TableDataInfo<IsfdMetadataManagementVo> list(IsfdMetadataManagementBo bo, PageQuery pageQuery) {
        return isfdMetadataManagementService.queryPageList(bo, pageQuery);
    }

    /**
     * 元数据管理-数据预览
     */
    @SaCheckPermission("isfd:metadataManagement:dataPreview")
    @GetMapping("/dataPreview")
    public String getDataPreview(@Validated(AddGroup.class) @RequestBody IsfdMetadataManagementBo bo) {
        return isfdMetadataManagementService.getDataPreview(bo);
    }

    /**
     * 元数据管理-数据映射
     */
    @SaCheckPermission("isfd:metadataManagement:dataMapping")
    @GetMapping("/dataMapping")
    public String getDataMapping(@Validated(AddGroup.class) @RequestBody IsfdMetadataManagementBo bo) {
        return isfdMetadataManagementService.getDataMapping(bo);
    }


    /**
     * 元数据管理-数据质量
     */
    @SaCheckPermission("isfd:metadataManagement:excelQuality")
    @Log(title = "元数据管理-数据质量", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/excelQuality")
    public R<Map> assessExcelQuality(@RequestParam("file") MultipartFile file,
                                     @RequestParam(value = "timeCheck") String timeCheck,
                                     @RequestParam(value = "outlierCheck") String outlierCheck) {
        return R.ok("操作成功", isfdMetadataManagementService.assessExcelQuality(file, timeCheck, outlierCheck));
    }

    /**
     * 导出元数据管理-数据管理列表
     */
    @SaCheckPermission("isfd:metadataManagement:export")
    @Log(title = "元数据管理-数据管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IsfdMetadataManagementBo bo, HttpServletResponse response) {
        List<IsfdMetadataManagementVo> list = isfdMetadataManagementService.queryList(bo);
        ExcelUtil.exportExcel(list, "元数据管理-数据管理", IsfdMetadataManagementVo.class, response);
    }

    /**
     * 获取元数据管理-数据管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:metadataManagement:query")
    @GetMapping("/{id}")
    public R<IsfdMetadataManagementVo> getInfo(@NotNull(message = "主键不能为空")
                                               @PathVariable String id) {
        return R.ok(isfdMetadataManagementService.queryById(id));
    }

    /**
     * 获取要素库表结构
     */
    @SaCheckPermission("isfd:metadataManagement:tableInfo")
    @GetMapping("/tableInfo")
    public R<List<IsfdMetadataManagementTableInfoVo>> tableInfo(@RequestParam(value = "businessId", required = false) Long businessId,
                                                                @RequestParam(value = "databaseId", required = false) Long databaseId,
                                                                @RequestParam(value = "tableName", required = false) String tableName) {
        return R.ok(isfdMetadataManagementService.tableInfo(businessId, databaseId, tableName));
    }

    /**
     * 确认导入
     */
    @SaCheckPermission("isfd:metadataManagement:save")
    @Log(title = "确认导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Long> save(@RequestParam("file") MultipartFile file,
                        @RequestParam(value = "mapFields") String mapFields,
                        @RequestParam(value = "businessId") Long businessId,
                        @RequestParam(value = "databaseId") Long databaseId,
                        @RequestParam(value = "dataOwnerId") String dataOwnerId,
                        @RequestParam(value = "dataOwnerName") String dataOwnerName,
                        @RequestParam(value = "dataSafeLevel") String dataSafeLevel,
                        @RequestParam(value = "dataRetentionPolicy") String dataRetentionPolicy,
                        @RequestParam(value = "dataQualityId", required = false) String dataQualityId,
                        @RequestParam(value = "dataQuality", required = false) String dataQuality) {
        IsfdMetadataManagementBo bo = new IsfdMetadataManagementBo();
        bo.setBusinessId(businessId);
        bo.setDatabaseId(databaseId);
        bo.setDataOwnerId(dataOwnerId);
        bo.setDataOwnerName(dataOwnerName);
        bo.setDataSafeLevel(dataSafeLevel);
        bo.setDataRetentionPolicy(dataRetentionPolicy);
        bo.setDataQuality(dataQuality);
        bo.setDataQualityId(dataQualityId);
        bo.setMapFields(mapFields);
        return R.ok(isfdMetadataManagementService.save(file, bo));
    }
}
